package com.gt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.SicknessMenu;
import com.gt.admin.mapper.SicknessMenuMapper;
import com.gt.admin.service.ISicknessMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class SicknessMenuServiceImpl extends ServiceImpl<SicknessMenuMapper, SicknessMenu> implements ISicknessMenuService {

    @Autowired
    private SicknessMenuMapper sicknessMenuMapper;

    @Override
    public List<SicknessMenu> selectList(SicknessMenu sicknessMenu){
        LambdaQueryWrapper<SicknessMenu> queryWrapper = Wrappers.lambdaQuery(sicknessMenu);
        queryWrapper.orderByAsc(SicknessMenu::getSort);
        return list(queryWrapper);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<SicknessMenu> buildMenuTree(List<SicknessMenu> menus)
    {
        List<SicknessMenu> returnList = new ArrayList<SicknessMenu>();
        List<Long> tempList = new ArrayList<Long>();
        for (SicknessMenu sicknessMenu : menus)
        {
            tempList.add(sicknessMenu.getSickId());
        }
        for (Iterator<SicknessMenu> iterator = menus.iterator(); iterator.hasNext();)
        {
            SicknessMenu sicknessMenu = (SicknessMenu) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(sicknessMenu.getParentId()))
            {
                recursionFn(menus, sicknessMenu);
                returnList.add(sicknessMenu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 得到子节点列表
     */
    private List<SicknessMenu> getChildList(List<SicknessMenu> list, SicknessMenu t)
    {
        List<SicknessMenu> tlist = new ArrayList<SicknessMenu>();
        Iterator<SicknessMenu> it = list.iterator();
        while (it.hasNext())
        {
            SicknessMenu n = (SicknessMenu) it.next();
            if (n.getParentId().longValue() == t.getSickId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SicknessMenu> list, SicknessMenu t)
    {
        // 得到子节点列表
        List<SicknessMenu> childList = getChildList(list, t);
        t.setChildrenList(childList);
        for (SicknessMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SicknessMenu> list, SicknessMenu t)
    {
        return getChildList(list, t).size() > 0;
    }

}
